package com.jy.event.domain;

import com.jy.event.enums.OrderPayChannel;
import com.jy.event.util.UuidUtil;
import lombok.Data;
import lombok.NoArgsConstructor;

import javax.persistence.*;
import java.math.BigDecimal;
import java.util.Date;

/**
 * Created by lihu on 2017/12/18.
 */
@Entity
@Data
@NoArgsConstructor
public class ElectronicBill {

    @Id
    @Column(length = 50)
    private String id;

    private String storeId;


    private String mchId;

    //总订单数
    private int totalSingular;


    //总销售额
    private int totalAmount;

    //总退单额
    private int refundAmount;

    //总手续费
    private int serviceChargeAmount;

    //总实收金额
    private int totalAmountIncome;

    //日期
    private Date date;

    @Column(insertable = false, updatable = false, columnDefinition = "TIMESTAMP(3) DEFAULT CURRENT_TIMESTAMP(3)")
    private Date addTime;
    @Column(insertable = false, updatable = false, columnDefinition = "TIMESTAMP(3) DEFAULT CURRENT_TIMESTAMP(3) ON UPDATE CURRENT_TIMESTAMP(3)")
    private Date updateTime;

    @Enumerated(EnumType.STRING)
    private OrderPayChannel payChannel;

    public ElectronicBill(String storeId, String mchId, Date date, String[] total) {
        this.id = UuidUtil.getUuid();
        this.date = date;
        this.storeId = storeId;
        this.mchId = mchId;

        this.totalSingular = Integer.parseInt(total[0]);
        this.totalAmount = (int) (Double.parseDouble(total[1]) * 100);
        this.refundAmount = (int) (Double.parseDouble(total[2]) * 100);
        this.serviceChargeAmount = (int) (Double.parseDouble(total[4]) * 100);
        this.totalAmountIncome = (int) (Double.parseDouble(total[5]) * 100);
        this.payChannel = OrderPayChannel.HUI_SHANG;
    }

    public ElectronicBill(String storeId, String mchId, int totalAmount, Date date) {
        this.id = UuidUtil.getUuid();
        this.storeId = storeId;
        this.mchId = mchId;
        this.totalAmount = totalAmount;
        this.date = date;
        this.payChannel = OrderPayChannel.SAO_BEI;
    }

    public ElectronicBill(StorePay storePay, Date saveDate, SuiXingFuResult settleResult) {
        this.id = UuidUtil.getUuid();
        this.storeId = storePay.getStoreId();
        this.mchId = storePay.getMchId();
        this.date = saveDate;

        this.serviceChargeAmount = BigDecimal.valueOf(settleResult.getTotalFee()).multiply(BigDecimal.valueOf(100)).intValue();

        int total = BigDecimal.valueOf(settleResult.getTotalAmount()).add(BigDecimal.valueOf(settleResult.getTotalFee())).multiply(BigDecimal.valueOf(100)).intValue();

        this.totalAmount = total;
        this.totalAmountIncome = total;

        this.payChannel = OrderPayChannel.SUI_XIN_FU;
    }
}
